fExpr(Term)    :- fTerm(Term).
fExpr(Exp+Term):- fExpr(Exp), fTerm(Term).
fExpr(Exp-Term):- fExpr(Exp), fTerm(Term).

fTerm(Factor)      :- fFactor(Factor).
fTerm(Term*Factor) :- fTerm(Term), fFactor(Factor).
fTerm(Term/Factor) :- fTerm(Term), fFactor(Factor).

fFactor([Exp]) :- fExpr(Exp).
fFactor(Num) :- number(Num).

%valutatore
evalExpr(Term,Value) :- evalTerm(Term,Value).
evalExpr(Exp+Term, Value) :- evalExpr(Exp, Value1), evalTerm(Term, Value2),
	sum(Value1,Value2,Value).
evalExpr(Exp-Term, Value) :- evalExpr(Exp, Value1), evalTerm(Term, Value2),
	subtract(Value1,Value2,Value).

evalTerm(Factor, Value) :- evalFactor(Factor, Value).
evalTerm(Term*Factor, Value) :- evalTerm(Term, Value1),
	evalFactor(Factor, Value2),multiply(Value1,Value2,Value).
evalTerm(Term/Factor, Value) :- evalTerm(Term, Value1),
	evalFactor(Factor, Value2),divide(Value1,Value2,Value).

evalFactor([Exp], Value) :- evalExpr(Exp, Value).
evalFactor(N, N) :- valueof(N,_).


sum(Value1, Value2, Value) :-Value is Value1+Value2.

subtract(Value1,Value2,Value):-Value is Value1-Value2.
multiply(Value1,Value2,Value):-Value is Value1*Value2.
divide(Value1,Value2,Value):-Value is Value1/Value2.

valueof(N,0):-number(N).

